//
//  UIImage+image.m
//  BaiSiBuDeJie
//
//  Created by 胡锦涛 on 2018/11/30.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "UIImage+image.h"

@implementation UIImage (image)
// 在周边加一个边框为1的透明像素
- (UIImage *)imageAntialias
{
    CGFloat border = 1.0f;
    CGRect rect = CGRectMake(border, border, self.size.width-2*border, self.size.height-2*border);
    
    UIImage *img = nil;
    
    UIGraphicsBeginImageContext(CGSizeMake(rect.size.width,rect.size.height));
    [self drawInRect:CGRectMake(-1, -1, self.size.width, self.size.height)];
    img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIGraphicsBeginImageContext(self.size);
    [img drawInRect:rect];
    UIImage* antiImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return antiImage;
}

//UIImage切圆角
- (UIImage *)roundImage {
    // 1.开启图形上下文
    UIImage * image = self;
    // 比例因素:当前点与像素比例
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    // 2.描述裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 3.设置裁剪区域;
    [path addClip];
    // 4.画图片
    [image drawAtPoint:CGPointZero];
    // 5.取出图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    // 6.关闭上下文
    UIGraphicsEndImageContext();
    
    return image;
}
@end
